package br.com.lh.urabuilder.business
{
	import br.com.lh.urabuilder.domain.view.UraElement;
	import br.com.lh.urabuilder.view.ActionComponent;
	import br.com.lh.urabuilder.view.arrow.ArrowContainer;
	
	import mx.collections.ArrayCollection;
	import mx.containers.Canvas;
	import mx.core.UIComponent;

	public class ViewManagerImpl implements ViewManager
	{
		
		private var parent:UIComponent;
		[Bindable]
		public var componentList:ArrayCollection = new ArrayCollection();
		private var selectedList:ArrayCollection= new ArrayCollection();
		
		public function ViewManagerImpl(parent:UIComponent)	{
			this.parent = parent;
		}

		public function getComponents():ArrayCollection	{
			return componentList;
		}
		
		public function getSelectedComponents():ArrayCollection	{
			return selectedList;
		}
		
		public function selectComponent(component:UraElement, select:Boolean,keepSelection:Boolean):void{
			if (select && selectedList.contains(component)){
				return;
			}
			if (!keepSelection){
				removeAllFocus();
				selectedList.removeAll();
			}
			component.setSelected(select);
			if (select){
				addToList(component);
			}else{
				removeFromList(component);
			}
		}
	
		private function removeAllFocus():void{
			for (var i:int; i< selectedList.length; i++){
				UraElement(selectedList.getItemAt(i)).setSelected(false);	
			}	
		}
	
		private function addToList(component:UraElement):void{
			selectedList.addItem(component);
			for each (var element:UraElement in component.children()){
				selectedList.addItem(element);
			}
		}
		
		private function removeFromList(component:UraElement):void{
			var index:int =selectedList.getItemIndex(component);
			if (index != -1){
				selectedList.removeItemAt(index);
			}
			for each (var element:UraElement in component.children()){
				index =selectedList.getItemIndex(element);
				if (index!= -1){
					selectedList.removeItemAt(index);
				}	
			}
			
		}
			
		public function insertComponent(component:ActionComponent):void{
			componentList.addItem(component);
			parent.addChild(component);
		}
		
		public function removeComponent(component:UraElement):void{
//			selectComponent(component,false,true);
			component.remove();
			var index:int = componentList.getItemIndex(component);
			if (index != -1){
				componentList.removeItemAt(index);
			}
			if (component is ActionComponent){
				//DONT KNOW WHY???
				if (parent.contains(ActionComponent(component))){
				parent.removeChild(ActionComponent(component));
				}
			}
			else{
				//TODO: Como a lista esta implementada sem ser SET pode dar erro na deleção de uma seta dentro de um retangulo,
				//pois ela é um mesmo objeto para a ida e a volta.
				if (parent.contains(ArrowContainer(component))){
					parent.removeChild(ArrowContainer(component));
				}
			}
		}
		
		public function selectAll(select:Boolean):void{
			for (var i:int; i< componentList.length; i++){
				selectComponent(UraElement(componentList.getItemAt(i)),select,true);	
			}
		}
		
		public function drawArrow(startComponent:ActionComponent,endComponent:ActionComponent,text:String = ""):ArrowContainer{
			return new ArrowContainer(startComponent,endComponent,text);
		}
		
		public function removeAll():void{
			for (var i:int=componentList.length-1; i>=0; i--){
			  removeComponent(UraElement(componentList.getItemAt(i)));
			  
			}
			Canvas(parent).removeAllChildren();
		}
		
		public function getComponentById(id:int):ActionComponent{
			for (var i:int = 0;i < componentList.length;++i){
	    		if (componentList.getItemAt(i).id == id){
	    			return componentList.getItemAt(i) as ActionComponent;
	    		}
	    	}
	    	return null;
		}
		
		public function zoom(zoomIn:Boolean =true,scaleValue:Number= 1.2):Number{
			var components:ArrayCollection = getComponents();
			if (components.length == 0){
				return 1;
			}
			var oldScale:Number = UIComponent(components.getItemAt(0)).scaleX;
			var zoomValue:Number = zoomIn ? 1/scaleValue : scaleValue;
			for each (var element:UIComponent in components){
				doZoom(element,zoomValue);
			}
			return oldScale;
		}
		
		
		private function doZoom(element:UIComponent,scaleValue:Number):void{
			element.scaleX = element.scaleX * scaleValue;
			element.scaleY = element.scaleY * scaleValue;
		}
		
		
	}
}
